package code.twopoint;

import java.util.Arrays;

public class 有序数组的平方java {
    public static void main(String[] args) {
        int[] nums = {-4, -1, 1, 3, 10};
        System.out.println(Arrays.toString(sortedSquares(nums)));
    }

    public static int[] sortedSquares(int[] nums) {
        int[] ans = new int[nums.length];
        int right = nums.length - 1;
        int left = 0;
        int pos = nums.length - 1;
        while (left <= right) {
            if (nums[left] * nums[left] > nums[right] * nums[right]) {
                ans[pos--] = nums[left] * nums[left];
                left++;
            } else {
                ans[pos--] = nums[right] * nums[right];
                right--;
            }
        }
        return ans;
    }
}
